﻿using COC.Constant;
using COC.Debug;
using COC.UIAction;
using UnityEngine;

using System.Collections;

namespace COC.CComponent
{
public class DragAndDrop : MonoBehaviour
{

    private bool _mouseState;

    private GameObject target;

    public Vector3 screenSpace;

    public Vector3 offset;

    private Ray choosingRay;
    private Ray collidedRay;

    // Use this for initialization

    void Start()
    {



    }



    // Update is called once per frame

    void Update()
    {

        // Debug.Log(_mouseState);

        if (Input.GetMouseButtonDown(0))
        {



            RaycastHit hitInfo;

            target = GetClickedObject(out hitInfo);

            if (target != null)
            {

                _mouseState = true;

                screenSpace = Camera.main.WorldToScreenPoint(target.transform.position);

                offset = target.transform.position - Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenSpace.z));
                
            }

        }

        if (Input.GetMouseButtonUp(0))
        {

            _mouseState = false;

        }

        if (_mouseState)
        {

            //keep track of the mouse position

            var curScreenSpace = new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenSpace.z);



            //convert the screen mouse position to world point and adjust with offset

            var curPosition = Camera.main.ScreenToWorldPoint(curScreenSpace) + offset;

            curPosition2 = curPosition;

            //update the position of the object in the world

            target.transform.position = curPosition;
            Rect rect = new Rect(curPosition.x - target.transform.localScale.x, curPosition.z + target.transform.localScale.z, Mathf.Abs(curPosition.x - target.transform.localScale.x), Mathf.Abs(curPosition.z + target.transform.localScale.z));
            width= Mathf.Abs(curPosition.x - target.transform.localScale.x);
            height = Mathf.Abs(curPosition.z + target.transform.localScale.z);
            Vector3 left = new Vector3((curPosition2.x - target.transform.localScale.x), curPosition2.y, curPosition2.z + target.transform.localScale.z);
            Vector3 right = new Vector3((curPosition2.x - target.transform.localScale.x) + width, curPosition2.y, curPosition2.z + target.transform.localScale.z);
            Vector3 top = new Vector3((curPosition2.x - target.transform.localScale.x) + width, curPosition2.y, curPosition2.z + target.transform.localScale.z + height);
            Vector3 down = new Vector3((curPosition2.x - target.transform.localScale.x), curPosition2.y, curPosition2.z + target.transform.localScale.z + height);
           // left=Camera.main.WorldToScreenPoint(left);
           // right=Camera.main.WorldToScreenPoint(right);
           // top=Camera.main.WorldToScreenPoint(top);
           // down=Camera.main.WorldToScreenPoint(down);

            DebugTool.DrawLine(left,right,Color.blue,2,true);
            DebugTool.DrawLine(right,top,Color.blue,2,true);
            DebugTool.DrawLine(top,down,Color.blue,2,true);
            DebugTool.DrawLine(left,down,Color.blue,2,true);


            //Debug.DebugTool.DrawLine(new Vector3((curPosition2.x - target.transform.localScale.x), curPosition2.y, curPosition2.z + target.transform.localScale.z), new Vector3((curPosition2.x - target.transform.localScale.x)+width, curPosition2.y, curPosition2.z + target.transform.localScale.z), Color.blue, 3f, true);
            //Debug.DebugTool.DrawLine(new Vector3((curPosition2.x - target.transform.localScale.x)+width, curPosition2.y, curPosition2.z + target.transform.localScale.z), new Vector3((curPosition2.x - target.transform.localScale.x)+width, curPosition2.y, curPosition2.z + target.transform.localScale.z + height), Color.blue, 3f, true);
            //Debug.DebugTool.DrawLine(new Vector3((curPosition2.x - target.transform.localScale.x)+width, curPosition2.y, curPosition2.z + target.transform.localScale.z+height), new Vector3((curPosition2.x - target.transform.localScale.x), curPosition2.y, curPosition2.z + target.transform.localScale.z+height), Color.blue, 3f, true);
            //Debug.DebugTool.DrawLine(new Vector3((curPosition2.x - target.transform.localScale.x), curPosition2.y, curPosition2.z + target.transform.localScale.z+height), new Vector3((curPosition2.x - target.transform.localScale.x), curPosition2.y, curPosition2.z + target.transform.localScale.z), Color.blue, 3f, true);
       
           
            
           // whetherDetectTargetObject(curPosition, TileMapDemo.buildingCollideFlag);

        }

    }
    Rect rect = new Rect();
    Vector3 curPosition2;
    float width;
    float height;
   






    GameObject GetClickedObject(out RaycastHit hit)
    {

        GameObject target = null;

         choosingRay= Camera.main.ScreenPointToRay(Input.mousePosition);

        if (Physics.Raycast(choosingRay.origin, choosingRay.direction * 10, out hit,Mathf.Infinity,~(1<<CommonConstants.PANEL_LAYER)))
        {

            target = hit.collider.gameObject;
            //*empty collider message if exsit name
            CampAction.setColliderInfo("");
            /*set current selected object name */
            CampAction.setSelectionObjectName(target.name);

        }



        return target;

    }

    private void whetherDetectTargetObject(Vector3 target,string detect)
    {
        collidedRay=new Ray(target,Vector3.down*100);
       // Debug.DrawRay(collidedRay.origin,collidedRay.direction*10,Color.blue);
        RaycastHit hit;
        if (Physics.Raycast(collidedRay, out hit,Mathf.Infinity))
        {
            if (hit.transform.gameObject.tag.Contains(TileMapDemo.buildingCollideFlag))
            {
               // Debug.DrawLine(collidedRay.origin,hit.point,Color.red);
                UnityEngine.Debug.Log("collider name ="+hit.collider.name);
                hit.transform.renderer.material.color = Color.clear;
                lastGM = hit.transform;
            }
        }
        else
        {
            if(lastGM!=null)
                lastGM.transform.renderer.material.color = Color.Lerp(Color.blue,Color.green,0.5f);
        }

    }
    Transform lastGM;
}

}